﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Common;
using IBll;
using IDal;
using Txy.IDal.Core;

namespace Bll
{
    /// <summary>
    /// 基础Bll接口的实现
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class BaseBll<T>: IBaseBll<T> where T : class,new()
    {
        public IBaseDal<T> CurrentDal { get; set; }
        public abstract void SetCurrentDal();//抽象方法：要求子类必须实现
        public IDalSession CurrentDalSession {get;set;}

        public BaseBll()
        {
            //*************************************************************************
            //Spring.Net工厂容器生产DalSession,这里是抽象类,子类属性注入会失败,所以手动注入(子类可以注入父类属性，但不能是抽象父类)
            //*************************************************************************
            CurrentDalSession = IocHelper.AutofacResolve<IDalSession>();
            //父类的构造函数，会在子类创建对象时被调用
            SetCurrentDal();
           
        }

        #region 增加
        public T Add(T entity)
        {
            CurrentDal.Add(entity);
            CurrentDalSession.SaveChanges();
            return entity;
        }
        #endregion

        #region 删除
        public bool Delete(T entity)
        {
            CurrentDal.Delete(entity);
            return CurrentDalSession.SaveChanges() > 0;
        }
        #endregion

        #region 修改
        public bool Update(T entity)
        {
            CurrentDal.Update(entity);
            return CurrentDalSession.SaveChanges() > 0;
        }
        #endregion

        #region 查询
        public T GetEntity(object id)
        {
            return CurrentDal.GetEntity(id);
        }

        public List<T> GetList()
        {
            return CurrentDal.GetList();
        }

        public List<T> GetListByPage(int pageSize, int pageIndex, out int total)
        {
            Expression<Func<T, bool>> whereLambda = a=>true;
            return CurrentDal.GetIQueryableByPage(pageSize,pageIndex,out total,whereLambda).ToList();
        }

        public List<T> GetListByPageOrderOne<TOrderType>(int pageSize, int pageIndex, out int total, Expression<Func<T, TOrderType>> orderbyLambda, bool isAsc)
        {
            Expression<Func<T, bool>> whereLambda = a => true;
            return CurrentDal.GetIQueryableByPage<TOrderType>(pageSize, pageIndex, out total, whereLambda, orderbyLambda, isAsc).ToList();
        }

        public IQueryable<T> GetIQueryable()
        {
            return CurrentDal.GetIQueryable();
        }

        public IQueryable<T> GetIQueryableByPage(int pageSize, int pageIndex, out int total)
        {
            Expression<Func<T, bool>> whereLambda = a => true;
            return CurrentDal.GetIQueryableByPage(pageSize, pageIndex, out total, whereLambda);

        }

        public IQueryable<T> GetIQueryableByPageOrderOne<TOrderType>(int pageSize, int pageIndex, out int total, Expression<Func<T, TOrderType>> orderbyLambda,bool isAsc)
        {
            Expression<Func<T, bool>> whereLambda = a => true;
            return CurrentDal.GetIQueryableByPage<TOrderType>(pageSize, pageIndex, out total, whereLambda,orderbyLambda,isAsc);
        }
        #endregion

    }
}
